package Ztree;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Test {


    public static void main(String[] args) {


        String path = "D:\\2021-01-27项目打包\\Multithread\\src\\main\\resources\\b2b_sit_bmo_md_parm_region.json";
        FileReader fileReader = new FileReader(path);
        String s = fileReader.readString();
        List<Region> allRegion = JSONUtil.toList(JSONUtil.parseArray(s), Region.class);
        Collections.sort(allRegion,new Comparator<Region>(){
            public int compare(Region x1, Region x2){
                return x1.getRegionCode().compareTo(x2.getRegionCode());
            }
        });
        List<String> collect = allRegion.stream().filter(y -> y.getRegionFullName() != null && y.getRegionFullName().split("/").length == 2).map(x -> x.getRegionFullName()).collect(Collectors.toList());
        System.out.println("size:"+ collect.size());
        collect.forEach(x ->{
            String a = "INSERT INTO service_area(`province`, `district`, `area`) VALUES (" ;
            String[] split = x.split("/");
            String c1 = "'" + split[0] +"',";
            String c2 = "'" + split[1] +"'";
            //String c3 = "'" + split[2] +"'";
            String b = a +  c1 + c2  + ")" + ";";
            System.out.println(b);
        });

//        List<RegionTree> regionTreeList = buildTree(allRegion, null, "2");
//
//        String json = JSONUtil.toJsonStr(regionTreeList).replace("\"children\":[],","").replace("\"children\":[]","");
//        System.out.println(json);
//        System.out.println(JSONUtil.toJsonPrettyStr(JSONUtil.parseObj(json)));
//       FileUtil.writeUtf8String(json, "/Users/hus/Desktop/b2b_sit_bmo_md_parm_region2.json");

    }


    /**
     * 构造父子级
     *
     * @param allRegion
     * @param pid
     * @return
     */
    private static List<RegionTree> buildTree(List<Region> allRegion, String pid, String type) {

        List<RegionTree> regionTreeList = new ArrayList<>();
        //获取省
        if (pid == null) {
            List<Region> regionList = allRegion.stream().filter(region -> region.getRegionLevel().equals(type)).collect(Collectors.toList());
            for (Region region : regionList) {
                RegionTree regionTree = new RegionTree();
                regionTree.setCode(region.getRegionCode());
                regionTree.setName(region.getRegionName());
                regionTree.setChildren(buildTree(allRegion, region.getRegionCode(), type));
                regionTreeList.add(regionTree);
            }
        } else {
            //获取省市
            List<Region> regionList = allRegion.stream().filter(region -> region.getParentRegionCode().equals(pid)).collect(Collectors.toList());
            for (Region region : regionList) {
                RegionTree regionTree = new RegionTree();
                regionTree.setCode(region.getRegionCode());
                regionTree.setName(region.getRegionName());
                regionTree.setChildren(buildTree(allRegion, region.getRegionCode(), type));
                regionTreeList.add(regionTree);
            }
        }
        return regionTreeList;
    }

}
